package com.tchintech.pay.view.fragment;

import android.content.Intent;
import android.graphics.Paint;
import android.os.Handler;
import android.os.Message;
import android.view.View;
import android.widget.TextView;

import com.tchintech.pay.R;
import com.tchintech.pay.delegate.fragment.ShareFragmentDelagate;
import com.tchintech.pay.frame.presenter.ActivityPresenter;
import com.tchintech.pay.frame.presenter.FragmentPresenter;
import com.tchintech.pay.model.entity.HttpCode;
import com.tchintech.pay.net.OkhttpClientTest;
import com.tchintech.pay.utils.AppConst;
import com.tchintech.pay.view.activity.AllMoneyActivity;
import com.tchintech.pay.view.activity.BalanceAccountActivity;
import com.tchintech.pay.view.activity.GeneralizeActivity;
import com.tchintech.pay.view.activity.RateActivity;

import org.json.JSONObject;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by Administrator on 2017/8/18 0018.
 */

public class ShareFragment extends FragmentPresenter<ShareFragmentDelagate> implements View.OnClickListener {
    private Intent intent = new Intent();
    private Handler handler;
    private String money_url = "Money.MyMonye";
    private Map<String, String> map;
    private String allM, usableM;
    private TextView tv_zsy, tv_txje, tv_tx, tv_jyjl;


    @Override
    protected void initView() {
        super.initView();
        viewDelegate.setOnClickListener(this, R.id.rela_agency, R.id.rela_share, R.id.rela_generalize, R.id.rela_rate, R.id.tv_tx, R.id.tv_jyjl);
        tv_zsy = viewDelegate.get(R.id.tv_zsy);
        tv_txje = viewDelegate.get(R.id.tv_txje);
        tv_jyjl = viewDelegate.get(R.id.tv_jyjl);
        tv_tx = viewDelegate.get(R.id.tv_tx);
        tv_tx.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_tx.getPaint().setAntiAlias(true);//抗锯齿

        tv_jyjl.getPaint().setFlags(Paint.UNDERLINE_TEXT_FLAG); //下划线
        tv_jyjl.getPaint().setAntiAlias(true);//抗锯齿
        handler();
        getMoney();
    }

    private void handler() {
        handler = new Handler() {
            @Override
            public void handleMessage(Message msg) {
                super.handleMessage(msg);
                hideProgressDialog();
                switch (msg.what) {
                    case HttpCode.networkabnormal:
                        showMsg("当前网络不可用");
                        break;
                    case HttpCode.http_failed:
                        showMsg("连接失败");
                        break;
                    case HttpCode.http_timeout:
                        showMsg("请求超时");
                        break;
                    case HttpCode.USER_MONEY:
                        try {
                            JSONObject json = new JSONObject(msg.obj.toString());
                            if (json.getString("ret").equals("1")) {
                                allM = json.getJSONObject("data").getString("allMoney");
                                usableM = json.getJSONObject("data").getString("usable");
                                ChangeValue();
                            } else {
                                showMsg("金额查询失败");
                            }
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                        break;
                }
            }
        };

    }

    /**
     * 处理返回值并展示
     */
    private void ChangeValue() {
        String[] str = allM.split("\\.");
        if (str.length < 2) {
            allM = allM + ".00";
        } else {
            if (str[1].length() < 2) {
                allM = allM + "0";
            }
        }
        tv_zsy.setText(allM + "元");
        if (usableM == null || usableM.equals("null")) {
            usableM = "0.00";
        }
        tv_txje.setText(usableM + "元");
    }


    /**
     * 当前用户的金额
     */
    private void getMoney() {
        map = new HashMap<String, String>();
        map.put("user_id", AppConst.getUser().getId());
        showProgressDialog("", "加载中，请稍后...");
        new OkhttpClientTest().path(getActivity(), handler, money_url, map, HttpCode.USER_MONEY);


    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.rela_rate:
                //推广
                intent.setClass(getActivity(), RateActivity.class);
                startActivity(intent);
                break;
            case R.id.rela_generalize:
                //费率
                intent.setClass(getActivity(), GeneralizeActivity.class);
                startActivity(intent);
                break;
            case R.id.tv_tx:
                //余额
                intent.setClass(getActivity(), BalanceAccountActivity.class);
                intent.putExtra("usableM", usableM);
                startActivity(intent);
                break;
            case R.id.tv_jyjl:
                //交易记录
                intent.setClass(getActivity(), AllMoneyActivity.class);
                intent.putExtra("state", 1);
                startActivity(intent);
                break;
            default:
                showMsg("正在开发，敬请期待...");
        }

    }

    @Override
    protected Class<ShareFragmentDelagate> getDelegateClass() {
        return ShareFragmentDelagate.class;
    }
}
